const API = require('../../utils/api')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab:['海报推送','视频课堂','文章推送'],
    tab:['海报推送','文章推送'],
    tab_current:0,
    material_list:[],
    video_list:[],
    article_list:[],
    page:[1,1,1]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let that = this
    app.login(userData)
    function userData(data,hasUserInfo){
      that.setData({
        userInfo:data,
        hasUserInfo:hasUserInfo
      })
      console.log( '是否登陆',hasUserInfo )
    }
    this.get_poster_classifyList()
  },

  /**
   * 获取海报分类
   */
  get_poster_classifyList:function(){
    let that = this
    API.get_poster_classifyList({
      mediaType:5,
      type: 1
    }).then( res => {
        if( res.code == 200 ){
            res.rows.map( (item,index) => {
              let material_list = that.data.material_list
              let category = {}
              category.id = ''
              category.name = ''
              category.mediaType = ''
              category.list = []
              material_list[index] = category
              that.setData({
                material_list
              })
              this.get_material_list( index,item ) 
            })
        }
    })
  },

  /**
   * 接口 - 获取海报列表
   */
  get_material_list:function(index,item){
    console.log(index, item)
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    API.get_material_list({
      categoryId: item.id,
      mediaType: item.mediaType,
      token: wx.getStorageSync('token'),
      pageNum: that.data.page[index].pageNum,
      pageSize: 10
    }).then( res => {
      if( res.code == 200 ){
        if( item.mediaType == 2 ){ //视频
          let video_list = that.data.video_list
          let category ={}
          if( categoryId == '1396997064583417856' ){
            category.name = '推广'
          }else if( categoryId == '1396997089753436160' ){
            category.name = '新闻'
          }else{
            category.name = '知识'
          }
          category.list = res.rows
          video_list.push(category)
          that.setData({
            video_list:video_list,
          })
          // console.log('视频',video_list)
        }else{
          let material_list = this.data.material_list

          material_list[index].id = item.id
          material_list[index].name = item.name
          material_list[index].mediaType = item.mediaType
          material_list[index].list = material_list[index].list.concat(res.rows)
          that.setData({
            material_list:material_list, 
          })
          console.log('海报', that.data.material_list)
        }
      }
      setTimeout( () => {
        wx.hideLoading()
      }, 1000)
    })
  },

  /**
   * 接口 - 文章列表
   */
  get_artcle_list:function(){
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    API.get_artcle_list({
      sign:0,//0:小程序文章 1：园区文章
    }).then( res => {
      if( res.code == 200 ){
        that.setData({
          article_list:res.rows
        })
      }
      setTimeout( () => {
        wx.hideLoading()
      }, 1000)
    })
  },

  //事件 - tab切换
  tabChange:function(e){
    console.log(e.currentTarget.id)
    if( this.data.tab_current == e.currentTarget.id ){ //节流
      return
    }
    this.setData({
      tab_current:e.currentTarget.id,
      material_list:[],
      video_list:[],
      article_list:[],
    })
    if( e.currentTarget.id == 0 ){
      //海报
      this.get_poster_classifyList()
    }else if( e.currentTarget.id == 1 ){
      //文章
      this.get_artcle_list()
    }else{
      //视频
      this.get_material_list('1396997064583417856',2) //推广
      this.get_material_list('1396997089753436160',2) //新闻
      this.get_material_list('1396997115502268416',2) //知识
    }
  },
  //路由 - 分享页面
  routeShareCanvas:function(e){
    // if( !this.data.hasUserInfo ){
    //   wx.showToast({
    //     title: '您还没有登录哦',
    //     icon:'error'
    //   })
    //   setTimeout( () => {
    //     wx.reLaunch({
    //         url: '/pages/login/login?back=/pages/share/share',
    //     })
    //   },1000)
    //   return
    // }
    let item = e.currentTarget.dataset.item
    // console.log(item)
    wx.navigateTo({
      url: './canvas/canvas?id=' + item.categoryId
    })
  },
  //路由 - 视频详情
  routeDetail:function(e){
    // if( !this.data.hasUserInfo ){
    //   wx.showToast({
    //     title: '您还没有登录哦',
    //     icon:'error'
    //   })
    //   setTimeout( () => {
    //     wx.reLaunch({
    //         url: '/pages/login/login?back=/pages/share/share',
    //     })
    //   },1000)
    //   return
    // }
    console.log(e.currentTarget,this.data.hasUserInfo)
    let type = e.currentTarget.id
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: './detail/detail?type=' + type + '&id=' + e.currentTarget.dataset.item.id,
    })
  },
})